/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.threadtask.basetask;

import com.xlx.openLink.base.SnmpException;
import com.xlx.openLink.entity.baseenity.MibSystem;
import com.xlx.openLink.entity.pojo.Device;
import com.xlx.openLink.enums.DevStatusEnum;
import com.xlx.openLink.enums.DevTypeEnum;
import com.xlx.openLink.service.DeviceService;
import com.xlx.openLink.snmp.base.SNMPTarget;
import com.xlx.openLink.uitls.Commuitls;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

@Slf4j
public class DevSearchTask implements Runnable {

    private ConcurrentLinkedQueue<Device> deviceList;
    private List<Device> devices;
    private DeviceService deviceService;

    public DevSearchTask(ConcurrentLinkedQueue<Device> deviceList, List<Device> devices, DeviceService deviceService) {
        this.deviceList = deviceList;
        this.devices = devices;
        this.deviceService = deviceService;
    }

    @Override
    public void run() {
        devices.forEach(device -> {
            if (Commuitls.ping(device.getDevIp())) {
                device.setDevStatus(DevStatusEnum.OFFLINE.getCode());//不在线
                device.setDevUpdateDate(new Date());
                deviceList.add(device);
            } else {
                //判断是否是有端口
                if (!device.getDevType().equalsIgnoreCase(DevTypeEnum.CameraType.getCode()) || !device.getDevType().equalsIgnoreCase(DevTypeEnum.UnkownType.getCode())) {
                    try {
                        MibSystem mibSystem = deviceService.getSysInfo(device); //获取设备信息
                        if (mibSystem != null) {
                            device.setDevDescor(mibSystem.getSysDescr());
                            device.setDevOid(mibSystem.getMappingOID());
                            device.setDevSysUpTime(String.valueOf(mibSystem.getSysUpTime()));
                            device.setDevSysName(mibSystem.getSysName());//获取系统名称
                            device.setDevUpdateDate(new Date());
                            deviceList.add(device);
                        }
                    } catch (SnmpException e) {
                        log.error("获取设备:" + device.getDevName() + "报错");
                        e.printStackTrace();
                    }

                }
            }
        });

    }


}
